import { withRouter } from 'next/router';
import { getID } from '@/utils';
// 组件
import List from '@/pages_components/pathPage/HotelList';
import Detail from '@/pages_components/pathPage/HotelDetail';
// ssr api
import { productList_SSR, productDetail_SSR } from '@/ssr';

const PathPage = ({ router, detail, detail_data, list, list_total }) => {
    const { asPath } = router;
    // 渲染列表页，第二页开始
    if(asPath.includes('/list_')){
        return <List router={router} list={list} total={list_total} />
    // 渲染详情
    }else {
        return <Detail router={router} ssr={detail} ssr_data={detail_data} />
    }
}

// ssr
PathPage.getInitialProps = async (res) => {
    const { asPath, query, query: { path, date } } = res;
    // 返回列表数据
    if(asPath.includes('/list_')){
        const _query = query;
        delete _query.path;
        const { 
            ssr: list, 
            total: list_total
        } = await productList_SSR('/hotel', { pathname: path, query: _query });
        return { list, list_total }
    // 返回详情数据
    }else{
        let [startDate, endDate] = ["", ""];
        if(date && date.includes('_')){
            startDate = date.split('_')[0];
            endDate = date.split('_')[1];
        }
        const { 
            ssr: detail, 
            ssr_data: detail_data
        } = await productDetail_SSR('/hotel/', getID(path), { startDate, endDate });
        return { detail, detail_data }
    }
}
export default withRouter(PathPage)